<?php
/**
 * Operation with dates
 *
 * @author den
 */
class Lemar_Date
{
	/** Date formats regarding Zend_Date formats */
	const MYSQL_DATE = 'yyyy-MM-dd';
	const MYSQL_DATETIME = 'yyyy-MM-dd HH:mm:ss';
	const SITE_DATE = 'MM/dd/yyyy';

	/**
	 * Returns current date in specified format
	 *
	 * @param string $sFormat
	 * @return string
	 */
	public static function getCurrentDate($sFormat = self::MYSQL_DATE)
	{
		$oDate = new Zend_Date();
		return $oDate->get($sFormat);
	}

	/**
	 * Converts date from one format to another
	 *
	 * @param string $sDate
	 * @param string $sFormatIn
	 * @param string $sFormatOut
	 * @return string
	 */
	public static function convertFormats($sDate, $sFormatIn, $sFormatOut)
	{
        $oDate = new Zend_Date($sDate, $sFormatIn);
		return $oDate->get($sFormatOut);
	}

	/**
	 * Returns dates difference in days
	 *
	 * @param string $sDate1
	 * @param string $sDate2
	 * @param string $sFormat1
	 * @param string $sFormat2
	 * @return int
	 */
	public static function getDatesDiff($sDate1, $sDate2, $sFormat1 = self::MYSQL_DATE, $sFormat2 = self::MYSQL_DATE)
	{
		$iSeconds1 = (int) self::convertFormats($sDate1, $sFormat1, Zend_Date::TIMESTAMP);
		$iSeconds2 = (int) self::convertFormats($sDate2, $sFormat2, Zend_Date::TIMESTAMP);
		return ceil(($iSeconds1 - $iSeconds2) / 86400);
	}
}